// miniprogram/pages/subPages/supplyRecords/supplyRecords.js
const db=wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsImg:[],//上传的图片
    goods:[
      {
        id:"goods01",
        price:22.99,
        name:'耳机',
        num:100,
        imgSrc:"https://t7.baidu.com/it/u=3616242789,1098670747&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601793654&t=b5ff4362e76a06d37c1489013fb31406"
      },
      {
        id: "goods02",
        price: 22.99,
        name: '耳机',
        num: 100,
        imgSrc: "https://t8.baidu.com/it/u=3571592872,3353494284&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601793654&t=7025a71fbe2dcc98bef773461d9d39ca"
      },
      {
        id: "goods03",
        price: 22.99,
        name: '耳机',
        num: 100,
        imgSrc: "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=975216563,2695721503&fm=11&gp=0.jpg"
      },
      {
        id: "goods04",
        price: 22.99,
        name: '耳机',
        num: 100,
        imgSrc: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=682895161,2056544872&fm=11&gp=0.jpg"
      },
      {
        id: "goods05",
        price: 22.99,
        name: '耳机',
        num: 100,
        imgSrc: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1825271931,3176680578&fm=26&gp=0.jpg"
      }
    ],
    select:false,
    state:"供货中",
    chance:["交易中","完成","取消"],//交易状态
    index:0,//交易状态
    supplyId:"",//供货单的id
    marketName:"",//卖场的名字
    
  },
  // 添加图片
  addImg:function(){
    var that=this
    wx.chooseImage({
      count: 4,
      sizeType: [],
      sourceType: [],
      success: function(res) {
        console.log(res)
        console.log(res.tempFiles[0].path)
        var length = res.tempFiles.length;
        for(var i=0;i<length;i++)
        {
          that.uploadImg(res.tempFiles[i].path)
          that.setData({
            goodsImg:[...that.data.goodsImg,res.tempFiles[i].path]
          })
        }

         wx.cloud.callFunction({
           name:"updateSupplyImg",
            data:{
              id:that.data.supplyId,
              supplypics:that.data.goodsImg
            },
            success:res=>{
              console.log(res)
            },fail:err=>{
              console.log(err)
            }
          })
      },
      fail: function(res) {
        wx.showToast({
          title: '图片添加失败!',
        })
      },
      complete: function(res) {},
    })
  },

// 上传图片
  uploadImg(fileUrl){
    wx.cloud.uploadFile({
      cloudPath:new Date().getTime()+'.png',//上传到云端的路径
      filePath:fileUrl,
      success:res=>{
        console.log("上传成功",res)
        wx.showToast({
          title: '上传成功',
        })

        this.setData({
          goodsImg:[...that.data.goodsImg,res.fileID]
        })
      },
      fail:err=>{
        console.log(err)
      }
    })
  },
  bindSelect() {
    this.setData({
      select: !this.data.select
    })
  },
  // 选择
  mySelect(e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      state: name,
      select: false
    })
  },
  // 确定按钮
  ascertain:function(){
    // 将上传的图片和交易状态改变

    wx.switchTab({
      url: '../../index/index',
    })
  },
  // 修改
  modify:function(){
   wx.navigateTo({
     url: '../gonghuo/gonghuo?supply='+this.data.supplyId,
   })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("数据",options)
    var {supplyId}=options
    this.setData({
      supplyId:supplyId
    })
    // 根据传递过来的供货单Id获取数据
    db.collection("supply").doc(supplyId).get({
      success:res=>{
        console.log(res)
        this.setData({
          goods:res.data.products,
          index:res.data.status,
          goodsImg:res.data.supplypics,
          marketName:res.data.marketName
        })
        console.log('图片',this.data.goodsImg)
      },
      fail:err=>{
        console.log(err)
      }
    })
  },

  // 删除图片
  delImg(e){
    console.log(e)
    var imgUrl=e.currentTarget.dataset.img;
    var index=e.currentTarget.dataset.index;
    var that=this;
    wx.cloud.deleteFile({
      fileList:[imgUrl],
      success:res=>{
        console.log(res)
        var newData=that.data.goodsImg.splice(index,1)
        console.log(that.data.goodsImg)
        console.log(newData)
        that.setData({
          goodsImg:that.data.goodsImg
        })
        console.log(that.data.goodsImg)
        wx.cloud.callFunction({
          name:"updateSupplyImg",
          data:{
            id:that.data.supplyId,
            supplypics:that.data.goodsImg
          },
          success:res=>{
            console.log(res)
            wx.showToast({
              title: '删除成功',
            })
          },fail:err=>{
            console.log(err)
            wx.showToast({
              title: '删除失败',
            })
          }
        })
      },fail:err=>{
        console.log(err)
      }
    })
  },


  handlerPicker(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    // 将交易状态更改到集合中
    var status=e.detail.value
    var id=this.data.supplyId;
    // console.log(supplyId)
    wx.cloud.callFunction({
      name:"updateSupplyStatus",
      data:{
        id:id,
        status:status
      },
      success:res=>{
        console.log(res)
      },
      fail:err=>{
        console.log(err)
      }
    })
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var length=this.data.goods.length;
    for(var i=0;i<length;i++)
    {
      console.log(this.data.goods[i].imgSrc)
      this.data.goodsImg[i]=this.data.goods[i].imgSrc
    }
    this.setData({
      goodsImg:this.data.goodsImg
    })
    console.log(this.data. goodsImg)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})